Golang encoding/xml.Encoder.Flush() function example
package encoding/xml
Flush flushes any buffered XML to the underlying writer. See the EncodeToken documentation for details about when it is necessary.
Golang encoding/xml.Encoder.Flush() function usage example
package main
import (
"encoding/xml"
"fmt"
"os"
"reflect"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
newtoken := xml.StartElement{
Name : xml.Name{
Space: "",
Local: "location",
},
}
if err := encoder.EncodeToken(newtoken); err != nil {
fmt.Println(err)
}
start := xml.StartElement{
Name: xml.Name{
Space: "",
Local: reflect.Indirect(reflect.ValueOf(v)).Type().Name(),
},
Attr: []xml.Attr{{xml.Name{"", "xmlns"}, "https://socketloop.com/xmldoc/2014-09-01/"}},
}
if err := encoder.EncodeElement(v, start); err != nil {
fmt.Println(err)
}
encoder.Flush() // <-- here
}
Reference :
Advertisement
Something interesting
Tutorials
+30k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+12.4k Elastic Search : Return all records (higher than default 10)
+5.6k Swift : Get substring with rangeOfString() function example
+8.2k Golang : HttpRouter multiplexer routing example
+9k Golang : Get SPF and DMARC from email headers to fight spam
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+14.5k How to automatically restart your crashed Golang server
+9.7k Golang : List available AWS regions
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+15.2k Golang : Get HTTP protocol version example
+10.6k Golang : Allow Cross-Origin Resource Sharing request
+48.5k Golang : Upload file from web browser to server